let User = require('../model/user')
let Company = require('../model/company')
let userController = require('../controller/user')
let result = require('../utils/result')
let format = require('../utils/format.js')
var util = require("util")
let md5 = require('md5')
let axios = require('axios')
function CompanyController(){

};
CompanyController.prototype.authFn=async (req,res)=>{
    let uid = req.body.uid;
    let name = req.body.name;
    let logo = req.body.logo;
    let license_img = req.body.license_img;
    let short_name = req.body.short_name;
    let address = req.body.address;
    let phone = req.body.phone;
    let contact_person = req.body.contact_person;
    let intro = req.body.intro;
    console.log(uid)
    console.log(req.body.uid)
    if(!uid){
        res.status(200).json(result(-1,'缺少必要参数 uid'))
        return;
    };
    if(!name){
        res.status(200).json(result(-1,'缺少必要参数 name'))
        return;
    };
    if(!logo){
        res.status(200).json(result(-1,'缺少必要参数 logo'))
        return;
    };
    if(!license_img){
        res.status(200).json(result(-1,'缺少必要参数 license_img'))
        return;
    };
    if(!address){
        res.status(200).json(result(-1,'缺少必要参数 address'))
        return;
    };
    if(!phone){
        res.status(200).json(result(-1,'缺少必要参数 phone'))
        return;
    };
    if(!contact_person){
        res.status(200).json(result(-1,'缺少必要参数 contact_person'))
        return;
    };
    if(!contact_person){
        res.status(200).json(result(-1,'缺少必要参数 contact_person'))
        return;
    };
    if(!intro){
        res.status(200).json(result(-1,'缺少必要参数 intro'))
        return;
    };
    let user = await User.findOne({where: {uid}});
    let type = user.type
    Company.findOne({where:{uid:uid}}).then(company=>{
        if(company){
            res.status(200).json(result(-1,'企业已认证过了'))
        }else{
            Company.create({
                uid,name,logo,license_img,short_name,address,phone,contact_person,intro,type
            }).then(rsp=>{
                res.status(200).json(result(0,'企业认证成功'))
            }).catch(err=>{
                res.status(200).json(result(-1,'企业认证失败'))

            })
        }
    })
}
CompanyController.prototype.company=async (req,res)=>{
    let uid = req.body.uid;
    if(!uid){
        res.status(200).json(result(-1,'无uid'))
        return;
    }
    Company.findOne({where:{uid}}).then(company=>{
        if(company){
            res.status(200).json(result(0,'success',company))

        }else{
            res.status(200).json(result(-1,'服务器错误'))
        }
    }).catch(err=>{
        res.status(200).json(result(-1,'服务器错误'))
    })
}
let companyController = new CompanyController();
module.exports = companyController
